/**
*@Author?: QWP
*@Description?: 定位的共享数据
*@Date 2023-09-09 14?:58
*/
import { defineStore } from 'pinia'
import request from '@/http'
import axios from 'axios'

export interface ILocation {
	ip?: string;
	network?: string;
	version?: string;
	city?: string;
	region?: string;
	region_code?: string;
	country?: string;
	country_name?: string;
	country_code?: string;
	country_code_iso3?: string;
	country_capital?: string;
	country_tld?: string;
	continent_code?: string;
	in_eu?: boolean;
	postal?: any;
	latitude?: number;
	longitude?: number;
	timezone?: string;
	utc_offset?: string;
	country_calling_code?: string;
	currency?: string;
	currency_name?: string;
	languages?: string;
	country_area?: number;
	country_population?: number;
	asn?: string;
	org?: string;
}

interface locationState {
  location?: ILocation, 
}

export const useLocationStore = defineStore('locationState', {
  state: (): locationState => {
    return {
      location: {}, 
    }
  },
  actions: {
    async setLocation() {
      if(Object.keys(this.location).length != 0) {
        return
      }
      try {
        const res = await axios.get('https://ipapi.co/json')
        if(res.data) {
          this.location = res.data
        }
      } catch (error) {}
    },
  },
  persist: true
})